package com.chat.db.nosql.redis.cluster;

import com.chat.db.DbLinkConfigBean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * 该类初级建造者脑子有点歪,所以缺失很多构造函数
 * 最重要的是默认值的都会被覆盖
 * 鄙人不才把默认构造函数删了就是怕大家滥用
 *
 */
public class JedisClusterConfig {

    // 定义主机端口分隔符 20211003
    // 指定属性交给 DbLinkConfigBean
    @Deprecated
    public static final String HOST_SEPARATOR = ";";
    @Deprecated
    public static final String PORT_SEPARATOR = ":";

    //集群redis连接
    private JedisCluster jedisCluster = null;

    //服务器地址+端口号
    private String hostsPort = "localhost";

    //端口号
    private Integer port = 6379;

    //密码
    private String passowrd = "test";

    //最大连接数
    private Integer maxTotal = 300;

    //最大空闲数
    private Integer maxIdle = 10;

    //最大允许等待空闲时间
    private Integer maxWait = 5000;

    //最大尝试次数
    private Integer maxAttempts = 5;

    //超时时间
    private Integer timeout = 3000;

    public JedisClusterConfig(String hostsPort, Integer port, String passowrd) {
        this.hostsPort = hostsPort;
        this.port = port;
        this.passowrd = passowrd;
    }

    public JedisClusterConfig(String hostsPort, Integer port, String passowrd, Integer maxTotal
            , Integer maxIdle, Integer maxWait, Integer maxAttempts, Integer timeout) {
        this.hostsPort = hostsPort;
        this.port = port;
        this.passowrd = passowrd;
        this.maxTotal = maxTotal;
        this.maxIdle = maxIdle;
        this.maxWait = maxWait;
        this.maxAttempts = maxAttempts;
        this.timeout = timeout;
    }

    private synchronized void initJedisCluster() {
        if (jedisCluster == null) {
            Set<HostAndPort> nodes = new HashSet<>();
            //解析获取redis节点
            if (hostsPort == null) {
                throw new NullPointerException();
            }

            // if (hostsPort.contains(":")) {
            //     //hostsPort模式 ip:port;
            //     String[] hostports = hostsPort.split(";");
            //     for (String hostport : hostports) {
            //         String[] hostAndPort = hostport.split(":");
            //         if (hostAndPort != null && hostAndPort.length == 2) {
            //             nodes.add(new HostAndPort(hostAndPort[0], Integer.valueOf(hostAndPort[1])));
            //         }
            //     }
            // } else {
            //     //hosts模式ip;ip
            //     String[] hosts = hostsPort.split(";");
            //     for (String host : hosts) {
            //         nodes.add(new HostAndPort(host, port));
            //     }
            // }
            // 20211203 更换原来机子添加处理
            String[] hosts = hostsPort.split(DbLinkConfigBean.HOST_SEPARATOR);
            for (String host : hosts) {
                String[] hostAndPort = host.split(DbLinkConfigBean.PORT_SEPARATOR);

                String ip = hostAndPort[0];
                int tp = port;

                if (ip.length() < 1) continue;

                if (hostAndPort.length > 1 && hostAndPort[1].length() > 0) {
                    tp = Integer.parseInt(hostAndPort[1]);
                }
                nodes.add(new HostAndPort(ip, tp));
            }

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(maxTotal);
            config.setMaxIdle(maxIdle);
            config.setMaxWaitMillis(maxWait);
            jedisCluster = new JedisCluster(nodes, timeout, 3000, maxAttempts, passowrd, config);
        }
    }

    //获取集群节点
    public JedisCluster getJedisCluster() {
        if (jedisCluster == null) {
            initJedisCluster();
        }
        return jedisCluster;
    }


}
